package org.gwu.seuss.ui.connection;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import org.gwu.seuss.communication.request.ClientRequest;
import org.gwu.seuss.communication.request.ServerResponse;

public abstract class ConnectionHandler {

	protected Socket socket;

	public abstract void makeConnection(String resources, String host, int port,
			char[] password) throws UnknownHostException, IOException;

	@SuppressWarnings("finally")
	public ServerResponse sendRequest(ClientRequest clientRequest)
			throws IOException {
		ServerResponse sr = new ServerResponse();
		ObjectOutputStream oos = new ObjectOutputStream(
				socket.getOutputStream());
		oos.writeObject(clientRequest);
		oos.flush();

		ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
		try {
			sr = (ServerResponse) ois.readObject();
		} finally {
			oos.close();
			socket.close();
			return sr;
		}
	}

}
